package com.ds.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.w3c.dom.Node;

/**
 *
 * @author dhisaxen
 */
public class ProfileDetails {

    private String profileName;
    private Node node;

    public int getNoOfProperties() {
        return this.getNodeMap().size();
    }

    public String getProfileName() {
        return profileName;
    }

    public void setProfileName(String profileName) {
        this.profileName = profileName;
    }

    public Map<String, String> getNodeMap() {
        Map<String, String> nodeMap = new HashMap<String, String>();
        traverseNodeAndCreateMap(this.node, nodeMap);
        return nodeMap;
    }

    public Set<String> getNodeNames() {
        return getNodeMap().keySet();
    }

    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }

    private void traverseNodeAndCreateMap(Node node, Map<String, String> nodeMap) {
        if (node.hasChildNodes()) {
//            for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            if (!node.getFirstChild().hasChildNodes() && node.getFirstChild().getNodeName().contains("#")) {
                nodeMap.put(node.getNodeName(), node.getTextContent());
            }
            for (int i = 0; i < node.getChildNodes().getLength(); i++) {
                traverseNodeAndCreateMap(node.getChildNodes().item(i), nodeMap);
            }

        } else {
            if (!node.getNodeName().contains("#")) {
                nodeMap.put(node.getNodeName(), node.getTextContent());
            }
        }
    }

    public void printAllValues() {
        for (String key : getNodeMap().keySet()) {
            System.out.println(key + ">>" + getNodeMap().get(key));
        }
    }
}
